package com.banciyuan.bcywebview.biz.search;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.fragment.BasePullLoadFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by squall on 2015/9/7.
 */
public class SearchInnerFragment extends BasePullLoadFragment {

    private TabLayout mTabLayout;
    private ViewPager pager;
    private String[] mTitles = new String[8];
    private List<SearchBaseFragment> mList = new ArrayList<>();
    private MyAdapter mAdapter;
    private int select;
    private SearchContentFragment searchContentFragment;
    private SearchContentFragment searchDailyFragment;
    private SearchContentFragment searchIllustFragment;
    private SearchContentFragment searchNovelFragment;
    private SearchContentFragment searchCosFragment;
    private SearchContentFragment searchGoodsFragment;

    private SearchGroupFragment searchGroupFragment;
    private SearchPersonFragment searchPersonFragment;
    private View rootView;
    private boolean is_init;

    public static final int PERSONINDEX = 7;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.search_inner_fragment, null, false);
        initArgs();
        return rootView;
    }

    public void setInit(Bundle args) {
        if (args != null) {
            if (!is_init) {
                getFragment(args);
                initUi(rootView);
                is_init = true;
            } else {
                updateFragment(args);
            }
        }
    }

    private void updateFragment(Bundle args) {

        String query = args.getString("query");
        select = args.getInt("select");
        for (int i = 0; i < mList.size(); i++) {
            mList.get(i).setQuery(query);
            if (pager.getCurrentItem() == i) {
                mList.get(i).reset();
            } else {
                mList.get(i).setDataInitiated(false);
                mList.get(i).resetLoading();
            }
        }
        pager.setCurrentItem(select);
    }

    private void getFragment(Bundle args) {
        searchContentFragment = new SearchContentFragment();
        searchDailyFragment = new SearchContentFragment();
        searchIllustFragment = new SearchContentFragment();
        searchNovelFragment = new SearchContentFragment();
        searchCosFragment = new SearchContentFragment();
        searchGroupFragment = new SearchGroupFragment();
        searchGoodsFragment = new SearchContentFragment();
        searchPersonFragment = new SearchPersonFragment();

        if (args != null) {
            select = args.getInt("select");
            Bundle content = new Bundle();
            content.putString("filter", "content");
            content.putString("query", args.getString("query"));
            searchContentFragment.setArguments(content);

            Bundle daily = new Bundle();
            daily.putString("filter", "daily");
            daily.putString("query", args.getString("query"));
            searchDailyFragment.setArguments(daily);

            Bundle drawer = new Bundle();
            drawer.putString("filter", "drawer");
            drawer.putString("query", args.getString("query"));
            searchIllustFragment.setArguments(drawer);

            Bundle writer = new Bundle();
            writer.putString("filter", "writer");
            writer.putString("query", args.getString("query"));
            searchNovelFragment.setArguments(writer);

            Bundle coser = new Bundle();
            coser.putString("filter", "coser");
            coser.putString("query", args.getString("query"));
            searchCosFragment.setArguments(coser);

            Bundle goods = new Bundle();
            goods.putString("filter", "goods");
            goods.putString("query", args.getString("query"));
            searchGoodsFragment.setArguments(goods);

            searchPersonFragment.setArguments(args);
            searchGroupFragment.setArguments(args);
        }

        mList.add(searchContentFragment);
        mList.add(searchDailyFragment);
        mList.add(searchIllustFragment);
        mList.add(searchNovelFragment);
        mList.add(searchCosFragment);
        mList.add(searchGoodsFragment);
        mList.add(searchGroupFragment);
        mList.add(searchPersonFragment);
    }

    @Override
    protected void initArgs() {
        mTitles[0] = getString(R.string.all_search);
        mTitles[1] = getString(R.string.daily);
        mTitles[2] = getString(R.string.draw);
        mTitles[3] = getString(R.string.write);
        mTitles[4] = getString(R.string.cos);
        mTitles[5] = getString(R.string.goods);
        mTitles[6] = getString(R.string.teamdetail_name);
        mTitles[7] = getString(R.string.user);
    }

    @Override
    protected void initUi(View rootview) {
        pager = (ViewPager) rootview.findViewById(R.id.pager);
        mTabLayout = (TabLayout) rootview.findViewById(R.id.tablayout);
        mAdapter = new MyAdapter(getChildFragmentManager());
        pager.setAdapter(mAdapter);
        pager.setOffscreenPageLimit(8);
        mTabLayout.setupWithViewPager(pager);
        pager.setCurrentItem(select);
    }

    class MyAdapter extends FragmentPagerAdapter {
        public MyAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mList.get(position);
        }

        @Override
        public int getCount() {
            return mList.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitles[position];
        }
    }

    public void gotoIndex(int index) {
        pager.setCurrentItem(index);
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {

    }
}
